package org.eerbaar.parser.cky;

import java.util.ArrayList;
import java.util.List;

import org.eerbaar.parser.core.def.Symbol;
import org.eerbaar.parser.core.exe.HasSymbol;
import org.eerbaar.parser.core.exe.SymbolInstance;

class Levels
    implements HasSymbol {

    private final Symbol[] input;
    private final List<Level> array = new ArrayList<Level>();

    public Levels(SymbolInstance[] input) {
        this.input = input;
        for (int i = 0; i < input.length; i++) {
            array.add(new Level(input, i));
        }
    }

    public Level getLevel(int index) {
        Level result = null;
        for (Level s : array) {
            if (s.getIndex() == index - 1) {
                result = s;
                break;
            }
        }
        return result;
    }

    @Override
    public SymbolInstance next() {
        SymbolInstance result = null;
        for (Level s : array) {
            result = s.next();
            if (result != null) {
                break;
            }
        }
        return result;
    }

    @Override
    public void remove(SymbolInstance symbol) {
        for (Level s : array) {
            s.remove(symbol);
        }
    }

    @Override
    public String toString() {
        String s = "";
        List<String> ls = new ArrayList<String>();
        for (Symbol ss : input) {
            ls.add(new Util().pad("" + ss));
        }
        s += ls.toString() + "\n";
        for (Level level : array) {
            s += level.toString();
        }
        return s;
    }

}
